﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using HashFoo.NhibTests.DomainFixtures.Enumerations;
using HashFoo.NhibTests.DomainFixtures.Models;
using HashFoo.SqlServer.Spatial;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HashFoo.NhibTests
{
    [TestClass]
    public class ForeignKeyColumnNameConventionTests
    {
        [TestMethod]
        public void generated_key_names_match_between_reference_and_has_many()
        {
            Helpers.RunDbTest(session =>
            {
                var grower = new Grower
                {
                    Name = "Mock",
                    FarmCount = 0,
                    FieldCount = 0,
                    GrowerType = GrowerType.Hillbilly
                };

                session.Save(grower);
                session.Flush();

                for (var i = 0; i < 4; i++)
                {
                    var field = new FieldBoundary
                    {
                        Boundary = Geography.FromPoint(0, 0),
                        FieldType = FieldType.Corn,
                        ParentGrower = grower
                    };

                    session.Save(field);
                }

                session.Flush();
                session.Clear();

                var actualGrower = session.Get<Grower>(grower.Id);
                Assert.AreNotEqual(0, actualGrower.Fields);
            });
        }

        [TestMethod]
        public void phantom_keys_are_not_created()
        {
            Helpers.RunDbTest(session =>
            {
                var user = new User
                {
                    UserName = "mockuser"
                };

                session.Save(user);
                user.UserRoles.Add(new UserRole
                {
                    User = user,
                    Role = UserRoleType.Admin
                });

                session.SaveOrUpdate(user);

                var grower = new Grower
                {
                    Name = "Mock",
                    FarmCount = 0,
                    FieldCount = 0,
                    GrowerType = GrowerType.Hillbilly,
                    Owner = user
                };

                session.Save(grower);
                session.Flush();
            });
        }


        [TestMethod]
        public void mapping_smoke_test()
        {
            Helpers.RunDbTest(session =>
            {
                for (var i = 0; i < 4; i++)
                {
                    var field = new FieldBoundary
                    {
                        Boundary = Geography.FromPoint(0, 0),
                        FieldType = FieldType.Corn,
                    };

                    session.Save(field);
                }

                session.Flush();
                session.Clear();
            });
        }

        [TestMethod]
        public void mapping_smoke_test_simple_object()
        {
            Helpers.NukeDatabase();

            var migrationManager = Helpers.CreateDefaultMigrationManagerWorker();
            migrationManager.Create();

            using (var sessionFactory = DomainFixtures.MockConfigurator.DefaultConfiguration.BuildSessionFactory())
            using (var session = sessionFactory.OpenSession())
            {
                for (var i = 0; i < 4; i++)
                {
                    var obj = new SimpleObjectMock
                    {
                        Name = "obj" + i
                    };

                    session.Save(obj);
                    session.Flush();
                }
            }
        }
    }
}
